import { loginForm, getUserInfo } from "@/api/user"
const state = {
    token: localStorage.getItem('houseToken') || "",
    userInfo: ''
}

const mutations = {
    // 存放token和用户信息
    setToken(store, data) {
        const { jwt, user } = data
        store.token = jwt
        store.userInfo = user
        localStorage.setItem('houseToken', jwt)
    },
    // 退出登录，清除token和用户信息
    loginOut(store) {
        store.token = ''
        store.userInfo = ''
        localStorage.setItem('houseToken', '')
    },
    // 存放用户信息
    setUserInfo(store, data) {
        store.userInfo = data
    }
}

const actions = {
    // 获取token和用户信息
    async getToken(store, data) {
        const res = await loginForm(data)
        store.commit('setToken', res)
    },
    // 获取用户信息
    async getUserInfo(store) {
        const res = await getUserInfo()
        store.commit('setUserInfo', res)
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}